package com.jyy.prefabricated.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.jyy.prefabricated.common.AjaxResult;
import com.jyy.prefabricated.dto.Pageable;
import com.jyy.prefabricated.entity.CourseCategory;
import com.jyy.prefabricated.service.CourseCategoryService;
import com.jyy.prefabricated.vo.ContentCategoryData;
import com.jyy.prefabricated.vo.CourseCategoryData;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

/**
 * 课程分类
 * @version 1.0.0
 * @author: zjj
 */
@Slf4j
@RequestMapping("/api")
@RestController
public class CourseCategoryController {

    @Autowired
    private CourseCategoryService courseCategoryService;

    /**
     * 新增课程分类
     * @param courseCategory
     * @return
     */
    @PostMapping("/course-category")
    public AjaxResult insert(@Valid @RequestBody CourseCategory courseCategory){
        log.info("Rest request to insert courseCategory:{}",courseCategory);
        int id = courseCategoryService.insert(courseCategory);
        CourseCategory result = courseCategoryService.selectById(id);
        return AjaxResult.success(result);
    }

    /**
     * 更新课程分类
     * @param courseCategory
     * @return
     */
    @PutMapping("/course-category")
    public AjaxResult update(@Valid @RequestBody CourseCategory courseCategory){
        log.info("Rest request to update courseCategory:{}",courseCategory);
        courseCategoryService.update(courseCategory);
        CourseCategory result = courseCategoryService.selectById(courseCategory.getId());
        return AjaxResult.success(result);
    }

    /**
     * 查询课程分类详情
     * @param id
     * @return
     */
    @GetMapping("/course-category/{id}")
    public AjaxResult selectById(@PathVariable Integer id){
        log.info("Rest request to find courseCategory by id:{}",id);
        CourseCategory result = courseCategoryService.selectById(id);
        return AjaxResult.success(result);
    }

    /**
     * 删除课程分类
     * @param id
     * @return
     */
    @DeleteMapping("/course-category/{id}")
    public AjaxResult deleteById(@PathVariable Integer id){
        log.info("Rest request to delete courseCategory by id:{}",id);
        int row = courseCategoryService.delete(id);
        if(row > 0){
            return AjaxResult.success();
        } else {
            return AjaxResult.error();
        }
    }

    /**
     * 分页查询课程分类
     * @param category 分类名称
     * @param pid  父级分类id
     * @param pageable 分页信息
     * @return
     */
    @GetMapping("/course-category")
    public AjaxResult selectByParams(@RequestParam(required = false,defaultValue = "") String category,@RequestParam(required = false) Integer pid,@RequestParam(required = false) Integer type, Pageable pageable){
        log.info("Rest request to find courseCategory by category:{},pid:{}",category,pid);
        PageHelper.startPage(pageable.getPageNum(), pageable.getPageSize());
        List<CourseCategory> categories = courseCategoryService.selectByParams(category, pid ,type);
        PageInfo<CourseCategory> result = new PageInfo<>(categories);
        return AjaxResult.success(result);
    }

    /**
     * 查询课程分类列表
     * @param category 分类名称
     * @param pid  父级分类id
     * @return
     */
    @GetMapping("/course-category-list")
    public AjaxResult selectByParams(@RequestParam(required = false,defaultValue = "") String category,@RequestParam(required = false) Integer pid ,@RequestParam(required = false) Integer type){
        log.info("Rest request to find courseCategory by category:{},pid:{}",category,pid);
        List<CourseCategory> result = courseCategoryService.selectByParams(category, pid ,type);
        return AjaxResult.success(result);
    }
    @GetMapping("/course-category-tree/{pid}")
    public AjaxResult selectByPId(@PathVariable Integer pid) {
        log.info("Rest request to find ContentCategory by pid:{}", pid);
        List<CourseCategoryData> result = courseCategoryService.selectTree(pid);
        return AjaxResult.success(result);
    }
}
